import React, { useEffect } from 'react';
import { notification } from 'antd';
import icon from './logo.jpg';

const Tip = function () {
  useEffect(() => {
    // 建立net连接
    window.websocket = new WebSocket('ws://192.168.8.211:8900/websocket/ALL');

    window.websocket.onopen = () => {
      console.log('websocket opening!');
    };
    
    window.websocket.onmessage = ({ data }) => {
      const title = '告警';
      const body = data || '';
      /* eslint-disable no-new */
      new window.Notification(title, { body });
          
      notification.warn({
        message: title,
        description: <div style={{ whiteSpace: 'pre-wrap' }}>{body}</div>,
        placement: 'topRight'
      });
    };
    
    window.websocket.onclose = () => {
      console.log('websocket close!');
    };
    
    window.websocket.onerror = (err) => {
      console.log(err, 'websocket error!');
    };
    
    return () => {
      window.websocket.close();
    };
  }, []);

  return '';
};

export default Tip;
